<template>
  <!-- 日常定价执行监控页面 -->
  <div class="pricingTask-monitor">
    <span class="formTitle"><i class="vertical-bar"></i>{{ title }}</span>
    <el-form
      :model="monitorForm"
      ref="monitorRef"
      label-width="110px"
      label-position="right"
    >
      <!-- <el-row>
        <el-col>定价任务监控</el-col>
      </el-row>       -->
      <el-row>
        <el-col :span="5" :offset="1">
          <el-form-item label="任务编号：" prop="taskCode">
            <el-input
              v-model="monitorForm.taskCode"
              maxlength="32"
              disabled
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5" :offset="1">
          <el-form-item label="任务名称：" prop="taskName">
            <el-input
              v-model="monitorForm.taskName"
              maxlength="32"
              disabled
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5" :offset="1">
          <el-form-item label="定价编号：" prop="pricingCode">
            <el-input
              v-model="monitorForm.pricingCode"
              maxlength="32"
              disabled
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5" :offset="1">
          <el-form-item label="数据日期：" prop="dataDate">
            <el-input
              v-model="monitorForm.dataDate"
              maxlength="32"
              disabled
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5" :offset="1">
          <el-form-item label="定价情景：" prop="sceneName">
            <el-input
              v-model="monitorForm.sceneName"
              maxlength="32"
              disabled
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5" :offset="1">
          <el-form-item label="情景版本：" prop="versionNum">
            <el-input
              v-model="monitorForm.versionNum"
              maxlength="32"
              disabled
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5" :offset="1">
          <el-form-item label="曲线确认状态：" prop="affirm">
            <el-input
              v-if="monitorForm.affirm == 1 ? true : false"
              v-model="affirmTrue"
              maxlength="32"
              disabled
            ></el-input>
            <el-col
              v-if="monitorForm.affirm == 0 ? true : false"
              :span="24"
              width="40%"
              >未确认 <el-button @click="affirmPop()">确认曲线</el-button>
            </el-col>
          </el-form-item>
        </el-col>
        <el-col :span="5" :offset="1">
          <el-form-item label="刷新频率：" prop="refreshRate">
            <el-select
              v-model="monitorForm.refreshRate"
              @change="onChange"
              style="width: 100%"
              placeholder="请选择"
            >
              <el-option
                v-for="item in refreshRateOption"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row class="container">
        <el-col style="margin-right: 10px" class="content-left">
          <upper-template :searchIcon="false" :configIcon="false">
            <!-- 操作按钮区 -->
            <template slot="buttonArea">
              <el-button @click="stop()">停止</el-button>
              <el-button @click="resume()">继续</el-button>
              <el-button @click="toResult()">查看结果</el-button>
              <el-button @click="toSubRecord()">子任务</el-button>
              <el-button @click="backIndex()">返回</el-button>
            </template>
          </upper-template>
        </el-col>
      </el-row>
      <el-table
        ref="multipleModelTable"
        :data="tableModelData"
        style="width: 100%"
        @row-click="authorizeModelFor"
      >
        <el-table-column
          width="55"
          type="selection"
          
        ></el-table-column>
        <el-table-column
          prop="dataDate"
          label="数据日期"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          prop="pdName"
          label="头寸表"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          prop="finish"
          label="已成功"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          prop="error"
          label="已失败"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          prop="running"
          label="正在定价"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          prop="init"
          label="未开始"
          :show-overflow-tooltip="true"
        ></el-table-column>
      </el-table>
    </el-form>
    <!-- 定价执行监控子任务页面 --------------------------- -->
    <el-dialog
      v-dialogDrag
      :title="subRecordtitleName"
      :visible.sync="subRecordDi"
      width="80%"
      top="2%"
      :append-to-body="true"
    >
      <positionsubRecordForm ref="recordForm" :subRecordFrom="recordFrom" />
      <div slot="footer">
        <!-- <el-button @click="saveFunc">确 定</el-button> -->
        <el-button @click="subRecordDi = false">返 回</el-button>
      </div>
    </el-dialog>
    <!-- 曲线确认页面 --------------------------- -->
    <el-dialog
      v-dialogDrag
      :title="affirmtitleName"
      :visible.sync="affirmDi"
      width="80%"
      top="2%"
      :append-to-body="true"
    >
      <positionaffirmForm ref="affirmForm" />
      <div slot="footer">
        <!-- <el-button @click="saveFunc">确 定</el-button> -->
        <el-button @click="curveBack()">返 回</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getDict } from "@/api/getDict";
import { pricingTask,publicManage_pub } from "@/api/index";
import positionaffirmForm from "./curveDataAffirm_index";
import positionsubRecordForm from "./pricingTask_subRecord";
export default {
  props: {
    //选中的值
    baseData: {
      type: Object,
      default: () => ({}),
    },
    title: String,
  },
  data() {
    return {
      affirmtitleName: "确认曲线",
      affirmDi: false,
      affirmTrue: "已确认",
      //刷新频率
      timer: null,
      //定价子任务界面
      recordFrom: {},
      subRecordtitleName: "定价子任务",
      subRecordDi: false,
      //列表数据
      tableModelData: [],
      monitorForm: {
        taskCode: this.baseData.taskCode,
        taskName: this.baseData.taskName,
        pricingCode: this.baseData.pricingCode,
        dataDate: this.baseData.dataDate,
        sceneName: this.baseData.sceneName,
        versionNum: this.baseData.versionNum,
        affirm:"0",
        refreshRate: "10",
      },
      refreshRateOption: {},
    };
  }, //在父页面是true的情况下在判断
  watch: {
    baseData: {
      handler: () => {
        this.monitorForm = {
          taskCode: this.baseData.taskCode,
          taskName: this.baseData.taskName,
          pricingCode: this.baseData.pricingCode,
          dataDate: this.baseData.dataDate,
          sceneName: this.baseData.sceneName,
          versionNum: this.baseData.versionNum,
          affirm:"0",
          refreshRate: "10",
        };
      },
      deep: true,
    },
  },
  components: {
    positionsubRecordForm,
    positionaffirmForm,
  },
  created() {
    this.getRefrshRate("ftp_refresh_rate");
    this.queryCount();
     this.curveAffirm();
  },
  computed: {},
  mounted() {
    
    // this.queryCount();
   
    // this.setTimeout();
    // this.timer = setInterval(() => {
    //   setTimeout(this.queryCount, 0);
    // }, 1000 * this.monitorForm.refreshRate);
  },
  methods: {
    toResult() {
      this.$set(this.baseData,"pdName","default")
      console.log("this.baseData"+this.baseData);
      this.$emit("toResult", this.baseData);
    },
    stop() {
      let data = {
        pricingCode: this.monitorForm.pricingCode,
      };
      pricingTask.pricingStop("'"+this.monitorForm.pricingCode+"'").then((res) => {
        if (res.code == 200) {
          return this.$message.success("停止定价成功");
        } else {
          return this.$message.warning("联调停止定价失败");
        }
      });
    },
    resume() {
      let data = {
        taskCode: this.monitorForm.taskCode,
        dataDate: this.monitorForm.dataDate,
      };
      pricingTask.pricingStop(data).then((res) => {
        if (res.code == 200) {
          return this.$message.success("继续定价成功");
        } else {
          return this.$message.warning("联调继续定价失败");
        }
      });
    },
    affirmPop() {
      this.affirmDi = true;
    },
    onChange(val) {
      console.log(this.monitorForm.refreshRate, "刷新频率");
      clearInterval(this.timer);
      this.timer = null;
      let data = this.monitorForm.refreshRate;
      console.log(data, "zhunbeichuangoq");
      this.setTimeout(data);
    },
    setTimeout(data) {
      console.log(data, "jinrudaozhge啥新的方法了");
      console.log();
      this.timer = setInterval(() => {
        setTimeout(this.queryCount, 0);
      }, 1000 * data);
    },
    toSubRecord() {
      this.recordFrom = {
        taskCode: this.monitorForm.taskCode,
        taskName: this.monitorForm.taskName,
        pricingCode: this.monitorForm.pricingCode,
        dataDate: this.monitorForm.dataDate,
        taskCode: this.monitorForm.taskCode,
      };
      console.log(this.recordFrom, "想要传递的值");
      this.$emit("toSubTasks", this.recordFrom);
    },
    queryCount() {
      console.log(
        "this.monitorForm.pricingCode:" + this.monitorForm.pricingCode
      );
      let data = "'" + this.monitorForm.pricingCode + "'";
      pricingTask.queryCount(data).then((res) => {
        console.log(res, "查到的数据");
        if (res.code == 200) {
          this.tableModelData = res.datas;
        } else {
          this.$message.error(res.message);
        }
      });
    },
    backIndex() {
      this.$emit("backIndex", "");
    },
    getRefrshRate(data) {
      getDict(data).then((res) => {
        if (res.code == 200) {
          this.refreshRateOption = res.datas;
        } else {
          this.$message.error(res.message);
        }
      });
    },
    //点击行选中
    authorizeModelFor(row) {
      if (this.$refs.multipleModelTable) {
        this.$refs.multipleModelTable.clearSelection();
      }
      this.$refs.multipleModelTable.toggleRowSelection(row, true);
    },
    // 检验是否都已经确认曲线
     curveAffirm() {
      publicManage_pub.checkAllAffirm().then(res => {
        if (res.code == 200) {
          this.monitorForm.affirm = 0;
        } else {
          this.monitorForm.affirm = 1;
        }
      });
    },
    curveBack(){
       this.affirmDi = false
       this.curveAffirm();
    }
  },
  beforeDestroy() {
    clearInterval(this.timer);
    this.timer = null;
  },
  
};
</script>
<style lang="scss" scoped>
.el-col-3 {
  width: 40%;
}

.formTitle {
  font-size: 14px;
  font-weight: 700;
  display: block;
  margin-bottom: 10px;
}
</style>